var app = getApp();
var address = require("./city.js");

Component({
    /**
     * 组件的属性列表
     */
    properties: {},

    /**
     * 组件的初始数据
     */
    data: {},

    ready() {
        var id = address.provinces[0].id;
        this.setData({
            provinces: address.provinces,
            citys: address.citys[id],
            areas: address.areas[address.citys[id][0].id],
        }, this.setValue);
    },

    /**
     * 组件的方法列表
     */
    methods: {
        setValue() {
            this.setData({
                value: [0, 0, 0]
            })
        },

        cityChange(e) {
            // 滚动后picker-view-column下标发生变化, 存储到变量中
            var value = e.detail.value
            let {
                provinces,
                citys
            } = this.data
                // 将当前的省市区下标存储到变量中
            var provinceNum = value[0]
            var cityNum = value[1]
            var areaNum = value[2]

            //判断"省"滚动条滚动是否发生, 如果发生则更新数据 
            if (this.data.value[0] !== provinceNum) {
                var id = provinces[provinceNum].id
                this.setData({
                        value: [provinceNum, 0, 0],
                        citys: address.citys[id],
                        areas: address.areas[address.citys[id][0].id]
                    })
                    // 城市滚动条滚动是否发生
            } else if (this.data.value[1] !== cityNum) {
                // 获取城市id号
                var id = citys[cityNum].id
                    // 根据城市id号获取区的集合
                this.setData({
                    value: [provinceNum, cityNum, 0],
                    areas: address.areas[citys[cityNum].id]
                })
            } else {
                this.setData({
                    value: [provinceNum, cityNum, areaNum]
                })
            }
        }
    },
});